﻿using System.Linq;
using System.Threading.Tasks;
using Identity.Models;
using Microsoft.AspNet.Identity;

namespace Identity.Infrastructure
{
    public class CustomUserValidator : UserValidator<AppUser>
    {
        public CustomUserValidator(UserManager<AppUser, string> manager) : base(manager)
        {
        }

        public override async Task<IdentityResult> ValidateAsync(AppUser item)
        {
            IdentityResult result = await base.ValidateAsync(item);
            if (!item.Email.ToLower().EndsWith("@example.com"))
            {
                var errors = result.Errors.ToList();
                errors.Add("Only examole.com emial addresses are allowed");
                result=new IdentityResult(errors);
            }
            return result;
        }
    }
}